package com.nw.game.service;


import com.alibaba.fastjson.JSONObject;
import com.nw.game.mapper.BackstageMapper;
import com.nw.game.mapper.UserMapper;
import com.nw.game.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class DataHandler{
    @Autowired
    UserMapper userMapper;
    @Autowired
    BackstageMapper backstageMapper;
    @Autowired
    HashData hashData;

    /**
     * describe 返回所有用户信息
     * @return String
     */
    public String findAll(){
        List<Map<String,Object>> userList = backstageMapper.findAll();
        return JSONObject.toJSONString(userList);
    }

    /**
     * 注册
     * operationCode 1
     * @return String
     * @author felix
     */
    public String signUp(JSONObject jsonObject){
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        User searchUser = userMapper.getUser(user.getUserName());
        System.out.println("search" + searchUser);
        // 0:正确 -3:账号已注册 500:服务器错误
        int code;
        if (searchUser != null) code = -3;
        else {
            System.out.println(user);
            int a = userMapper.addUser(user.getUserName(), user.getPassword());
            int b = userMapper.addProgress(user.getUserName());
            code =  a - b;
        }
        if(code == 0){
            return "{\"state\":true,\"operationCode\":2,\"errorCode\":0,\"msg\":\"注册成功\"}";
        }else if(code == -3){
            return "{\"state\":false,\"operationCode\":2,\"errorCode\":-3,\"msg\":\"账号已存在\"}";
        }else{
            return "{\"state\":false,\"operationCode\":2,\"errorCode\":500,\"msg\":\"服务器错误\"}";
        }
    }

    /**
     * 登陆
     * operationCode 3
     * @return String
     * @author felix
     */
    public String signIn(JSONObject jsonObject){
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        User searchUser = userMapper.getUser(user.getUserName());
        System.out.println("search" + searchUser);
        // 0:正确 -1:密码错误 -2:用户不存在
        int code =  searchUser == null ? -2 : searchUser.getPassword().equals(user.getPassword()) ? 0 : -1;
        if(code == 0){
            return "{\"state\":true,\"operationCode\":4,\"errorCode\":0,\"msg\":\"验证成功\"}";
        }else if(code == -1){
            return "{\"state\":false,\"operationCode\":4,\"errorCode\":-1,\"msg\":\"密码错误\"}";
        }
        else {
            return "{\"state\":false,\"operationCode\":4,\"errorCode\":-2,\"msg\":\"账户不存在\"}";
        }
    }

    /**
     * 客户端请求数据
     * operationCode 5
     * @author nicer
     */
    public String dataSend(JSONObject jsonObject){
        String key = jsonObject.getString("checkType");
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        String storyID = jsonObject.getString("storyID");
        String field = jsonObject.getString("result");
        long reqTime = System.currentTimeMillis() - 2000;
        if (userMapper.getUser(user.getUserName()).getPassword().equals(user.getPassword()) && HashData.exists(key,field)) {
            String timePayload = HashData.getHashValue(key,field);
            System.out.println(timePayload);
            assert timePayload != null;
            if (timePayload.isEmpty()) {
                return("{\"state\":false,\"operationCode\":6,\"checkType\":\"" + key + "\",\"result\":\"null\",\"storyID\":\"" + storyID + "\",\"errorCode\":-2,\"errorMsg\":\"key doesn't exists\"}");
            }
            long time = Long.parseLong(timePayload);
            if(time >= reqTime && time <= (reqTime + 12000)) {
                JSONObject jo = new JSONObject();
                jo.put("points",JSONObject.parseObject(getPoint(jsonObject)).getInteger("points")+10);
                jo.put("userName",user.getUserName());
                jo.put("password",user.getPassword());
                savaPoint(jo);
                return ("{\"state\":true,\"operationCode\":6,\"checkType\":\"" + key + "\",\"result\":\"" + field + "\",\"storyID\":" + storyID + "}");
            }
            else{
                return("{\"state\":false,\"operationCode\":6,\"checkType\":\"" + key + "\",\"result\":\"null\",\"storyID\":\"" + storyID + "\",\"errorCode\":-2,\"errorMsg\":\"key doesn't exists\"}");
            }
        }
        else {
            if(!userMapper.getUser(user.getUserName()).getPassword().equals(user.getPassword()))
                return("{\"state\":false,\"operationCode\":6,\"checkType\":\"" + key + "\",\"result\":\"null\",\"storyID\":\"" + storyID + "\",\"errorCode\":-1,\"errorMsg\":\"username or password error\"}");
            else
                return("{\"state\":false,\"operationCode\":6,\"checkType\":\"" + key + "\",\"result\":\"null\",\"storyID\":\"" + storyID + "\",\"errorCode\":-2,\"errorMsg\":\"key doesn't exists\"}");
        }
    }

    /**
     * 客户端保存积分
     * operationCode 7
     * @return String
     * @author felix
     */
    public String savaPoint(JSONObject jsonObject){
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        int points = jsonObject.getInteger("points");
        if(userMapper.getUser(user.getUserName()).getPassword().equals(user.getPassword()) && userMapper.savePoints(points, user.getUserName()) == 0) {
            return "{\"state\":true,\"operationCode\":8,\"errorCode\":0,\"msg\":\"录入成功\"}";
        }else {
            return "{\"state\":false,\"operationCode\":8,\"errorCode\":500,\"errorMsg\":\"服务器错误\"}";
        }
    }

    /**
     * 客户端请求积分
     * operationCode 9
     * @return String
     * @author felix
     */
    public String getPoint(JSONObject jsonObject){
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        int points = userMapper.getPoints(user.getUserName());
        if(userMapper.getUser(user.getUserName()).getPassword().equals(user.getPassword())) {
            return "{\"state\":true,\"operationCode\":10,\"points\":" + points + ",\"errorCode\":0,\"msg\":\"获取成功\"}";
        }
        else {
            return "{\"state\":false,\"operationCode\":10,\"result\":-1,\"errorCode\":500,\"errorMsg\":\"服务器错误\"}";
        }
    }

    /**
     * 客户端请求存档
     * operationCode 11
     * @return String
     * @author felix
     */
    public String savaStatus(JSONObject jsonObject){
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        String status = jsonObject.getString("status");
        if(userMapper.getUser(user.getUserName()).getPassword().equals(user.getPassword()) && userMapper.saveStatus(status,user.getUserName()) == 0) {
            return "{\"state\":true,\"operationCode\":12,\"msg\":\"录入成功\"}";
        }
        else {
            return "{\"state\":false,\"operationCode\":12,\"errorCode\":500,\"errorMsg\":\"服务器错误\"}";
        }

    }

    /**
     * 客户端请求存档数据
     * operationCode 13
     * @return String
     * @author felix
     */
    public String getStatus(JSONObject jsonObject){
        User user = new User(jsonObject.getString("userName"), jsonObject.getString("password"));
        String status = userMapper.getStatus(user.getUserName());
        if(userMapper.getUser(user.getUserName()).getPassword().equals(user.getPassword()) && status != null && !status.isEmpty()) {
            status =  "{\"state\":true,\"operationCode\":14,\"status\":"  + status  + ",\"errorCode\":0,\"msg\":\"获取成功\"}";
            JSONObject pa = JSONObject.parseObject(status);
            System.out.println("222");
            System.out.println(status);
            return pa.toString();
        }
        else {
            return "{\"state\":false,\"operationCode\":14,\"result\":-1,\"errorCode\":500,\"errorMsg\":\"服务器错误\"}";
        }
    }

    /**
     * 算法接受数据
     * operationCode
     * @author nicer
     */
    public String dataRec(JSONObject jsonObject){
        String ID = jsonObject.getString("ID");
        String Payload = jsonObject.getString("Payload");
        long getTime = System.currentTimeMillis();
        String time = String.valueOf(getTime);
        if(Payload.equals("-1") || Payload.equals(""))
            return null;
        if(ID.equals("0x01") || ID.equals("0x02") || ID.equals("0x03"))
        {
            HashData.addHash(ID,Payload,time);
        }
        return null;
    }
}

